package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.ActivityTypeDTO;
import com.fjwt.gz.db.entity.ActivityTemplateEntity;
import com.fjwt.gz.db.entity.ActivityTypeEntity;
import com.fjwt.gz.db.vo.ActivityTypeVO;
import com.fjwt.gz.service.mapper.ActivityTypeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 营销活动类型表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class ActivityTypeService extends ServiceImpl<ActivityTypeMapper, ActivityTypeEntity> {

    @Autowired
    private ActivityTypeMapper activityTypeMapper;
    @Autowired
    private ActivityTemplateService activityTemplateService;


    /**
     * 查询营销活动类型列表
     *
     * @param iPage
     * @param activityTypeDTO
     * @return
     */
    public IPage<ActivityTypeVO> queryList(IPage iPage, ActivityTypeDTO activityTypeDTO) {
        LambdaQueryWrapper<ActivityTypeEntity> queryWrapper = ActivityTypeEntity.gw();
        queryWrapper.like(StringUtils.isNotEmpty(activityTypeDTO.getActivityTypeName()),
                        ActivityTypeEntity::getActivityTypeName, activityTypeDTO.getActivityTypeName())
                .orderByDesc(ActivityTypeEntity::getCreatedAt);
        IPage<ActivityTypeVO> page = this.page(iPage, queryWrapper);;
        return page;
    }

    /**
     * 保存营销活动类型
     *
     * @param dto
     * @return
     */
    public void saveActivityType(ActivityTypeDTO dto) {
        if (StrUtil.isBlank(dto.getActivityTypeName()) || StrUtil.isBlank(dto.getActivityTypeNo())) {
            throw new BizException("请求参数不全，请检查！");
        }
        //判断活动编号是否已经存在
        ActivityTypeEntity entity = getById(dto.getActivityTypeNo());
        if (entity != null) {
            throw new BizException("该活动编号已存在，请重新输入！");
        }
        ActivityTypeEntity activityTypeEntity = BeanUtil.copyProperties(dto, ActivityTypeEntity.class);
        boolean flag = save(activityTypeEntity);
        if (!flag) {
            throw new BizException("保存失败！");
        }
    }

    /**
     * 更新营销活动类型
     *
     * @param dto 营销活动类型
     * @return
     */
    public void updateActivityType(ActivityTypeDTO dto) {
        if (StrUtil.isBlank(dto.getActivityTypeNo()) || StrUtil.isBlank(dto.getActivityTypeName())) {
            throw new BizException("请求参数不全，请检查！");
        }
        if (getById(dto.getActivityTypeNo()) == null) {
            throw new BizException("该记录不存在！");
        }
        dto.setCreatedAt(null);
        ActivityTypeEntity entity = BeanUtil.copyProperties(dto, ActivityTypeEntity.class);
        updateById(entity);
    }

    /**
     * 删除营销活动类型
     *
     * @param activityTypeNo 营销活动类型编号
     * @return
     */
    public void removeActivityType(String activityTypeNo) {
        if (StrUtil.isBlank(activityTypeNo)) {
            throw new BizException("请求参数不全，请检查！");
        }
        if (getById(activityTypeNo) == null) {
            throw new BizException("该记录不存在！");
        }
        //查询该活动类型是否已配置了活动模板
        List<ActivityTemplateEntity> list = activityTemplateService.list(ActivityTemplateEntity.gw()
                .eq(ActivityTemplateEntity::getActivityTypeNo, activityTypeNo));
        if (!list.isEmpty()) {
            throw new BizException("该活动类型已配置活动模板，不能删除！");
        }
        boolean flag = removeById(activityTypeNo);
        if (!flag) {
            throw new BizException("删除失败！");
        }
    }

    /**
     * 活动类型
     *
     * @param activityId
     * @return
     */
    public ActivityTypeEntity getActivityType(Long activityId) {
        return activityTypeMapper.getActivityType(activityId);
    }
}
